package com.hly.musicbackground.filter;

import com.hly.musicbackground.exception.BusinessException;
import com.hly.musicbackground.model.auth.service.VersionService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VersionHandlerInterceptor implements HandlerInterceptor {


    private final VersionService versionService;

    /**
     * 权限认证的拦截操作.
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                             Object object) throws Exception {
        String version = httpServletRequest.getHeader("versionKey");
        if (version == null || "".equals(version) || versionService.isForce(Integer.valueOf(version))){
            throw new BusinessException(Code.BUSINESS_ERR, "APP版本过低");
        }
        return true;
    }
}
